<template>
  <el-card class="tableCard">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="位置名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入位置名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级位置" prop="parentId">
        <treeselect
        style="width:200px;"
            v-model="queryParams.parentId"
            :options="areaOptions"
            :normalizer="normalizer"
            placeholder="请选择上级位置"
          />
      </el-form-item>
      <!-- <el-form-item label="祖籍列表" prop="ancestors">
        <el-input
          v-model="queryParams.ancestors"
          placeholder="请输入祖籍列表"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="所属部门" prop="deptId">
        <treeselect style="width:200px;"
            v-model="queryParams.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择归属部门"
          />
      </el-form-item>
      <el-form-item label="房间类型" prop="roomtypeId">
        <el-select
            v-model="queryParams.roomtypeId"
            placeholder="请选择房间类型"
            clearable
            size="small"
          >
            <el-option
              :label="item.roomtypeName"
              :value="item.roomtypeId"
              v-for="(item, index) in roomtypeList"
              :key="index"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="余床数" prop="remainbedNum">
        <el-input
          v-model.number="queryParams.remainbedNum"
          placeholder="请输入余床数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        class="right-toolbar"
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="areaList"
      row-key="areaId"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="位置名称" prop="areaName" />
      <el-table-column label="上级位置" align="center" prop="parentId">
        <template slot-scope="scope">
          <span>{{ areaTempList[scope.row.parentId] }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="祖籍列表" align="center" prop="ancestors" /> -->
      <el-table-column label="所属部门" align="center" prop="deptId">
        <template slot-scope="scope">
          <span>{{ deptTempList[scope.row.deptId] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="房间类型" align="center" prop="roomtypeId">
        <template slot-scope="scope">
          <span>{{ roomtypeTempList[scope.row.roomtypeId] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="余床数" align="center" prop="remainbedNum" />
      <el-table-column label="备注" align="center" prop="mark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            >新增</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改area对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="位置名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入位置名称" />
        </el-form-item>
        <el-form-item label="上级位置" prop="parentId">
          <treeselect
            v-model="form.parentId"
            :options="areaOptions"
            :normalizer="normalizer"
            placeholder="请选择上级位置"
          />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <treeselect
            v-model="form.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择归属部门"
          />
        </el-form-item>
        <el-form-item label="房间类型" prop="roomtypeId">
          <el-select
            v-model="form.roomtypeId"
            placeholder="请选择房间类型"
            clearable
            size="small"
          >
            <el-option
              :label="item.roomtypeName"
              :value="item.roomtypeId"
              v-for="(item, index) in roomtypeList"
              :key="index"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="余床数" prop="remainbedNum">
          <el-input
            v-model.number="form.remainbedNum"
            placeholder="请输入余床数"
          />
        </el-form-item>
        <el-form-item label="备注" prop="mark">
          <el-input
            v-model="form.mark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  listAreaPlus,
  listArea,
  getArea,
  delArea,
  addArea,
  updateArea,
} from "@/api/area";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listDept, treeselect } from "@/api/system/dept";
export default {
  name: "Area",
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // area表格数据
      areaList: [],
      // area树选项
      areaOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        areaName: null,
        parentId: null,
        ancestors: null,
        deptId: null,
        roomtypeId: null,
        remainbedNum: null,
        mark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 地点临时存储对象
      areaTempList: {},
      // 部门树选项
      deptOptions: undefined,
      // 部门临时存储列表
      deptTempList: {},
      // 房间类型列表
      roomtypeList: {},
      // 房间类型临时存储列表
      roomtypeTempList: {},
    };
  },
  beforeCreate() {
    // 解析部门选项
    treeselect().then((res) => {
      this.deptOptions = res.data.data;
    });
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询area列表 */
    getList() {
      this.loading = true;
      listAreaPlus(this.queryParams).then((response) => {
        // 方便展示
        response.data.data.areaList.forEach((item) => {
          this.areaTempList[item.areaId] = item.areaName;
        });
        this.roomtypeList = response.data.data.roomtypeList;
        response.data.data.roomtypeList.forEach((item) => {
          this.roomtypeTempList[item.roomtypeId] = item.roomtypeName;
        });
        response.data.data.deptList.forEach((item) => {
          this.deptTempList[item.deptId] = item.deptName;
        });

        this.areaList = this.handleTree(
          response.data.data.areaList,
          "areaId",
          "parentId"
        );
        this.getTreeselect();
        this.loading = false;
      });
    },
    /** 转换area数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.areaId,
        label: node.areaName,
        children: node.children,
      };
    },
    /** 查询area下拉树结构 */
    getTreeselect() {
      listArea().then((response) => {
        this.areaOptions = [];
        const data = { areaId: 0, areaName: "顶级节点", children: [] };
        data.children = this.handleTree(
          response.data.data,
          "areaId",
          "parentId"
        );
        this.areaOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        areaId: null,
        areaName: null,
        parentId: null,
        ancestors: null,
        deptId: null,
        roomtypeId: null,
        remainbedNum: null,
        mark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.areaId) {
        this.form.parentId = row.areaId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加area";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.areaId;
      }
      getArea(row.areaId).then((response) => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改area";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.areaId != null) {
            updateArea(this.form).then((response) => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArea(this.form).then((response) => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除area编号为"' + row.areaId + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function() {
          return delArea(row.areaId);
        })
        .then(() => {
          this.getList();
          this.$message.success("删除成功");
        });
    },
  },
};
</script>

<style>
.vue-treeselect__control {
  margin-top: 4px;
  height: 32px;
}
.vue-treeselect__input-container {
  height: 5px !important;
}
</style>
